package com.jiakai.cashinterfacemucon.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 时间工具类
 */
public class DateUtils {
    private static final String FORMAT_DATE = "yyyy-MM-dd";
    private static final String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";

    public static String getLongToDate(Long millSec){
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE);
        Date date= new Date(millSec);
        return sdf.format(date);
    }

    public static String getLongToDateTime(Long millSec){
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE_TIME);
        Date date= new Date(millSec);
        return sdf.format(date);
    }

    public static String getLongToDateCN(Long millSec){
        Date dt = new Date(millSec);
        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
        return dateFormat.format(dt);
    }

    /**
     * 获取时间字符串
     * @param date
     * @return
     */
    public static String parseDate(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE_TIME);
        return sdf.format(date);
    }

    /**
     * 根据时间字符串和具体格式转换成Date类型
     * @param dateStr
     * @param format
     * @return
     */
    public static Date parseString(String dateStr,String format) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 根据字符串转换成Date类型 yyyy-MM-dd格式
     * @param dateStr
     * @return
     */
    public static Date parseDateByString(String dateStr){
        return parseString(dateStr, FORMAT_DATE);
    }
    /**
     * 根据字符串转换成Date类型 yyyy-MM-dd HH:mm:ss格式
     * @param dateStr
     * @return
     */
    public static Date parseDateTimeByString(String dateStr){
        return parseString(dateStr, FORMAT_DATE_TIME);
    }

    public static Date parseDateByYearMonthDay(int year, int month, int day){
        String str = year + "-" + month +"-" + day;
        return parseDateByString(str);
    }

    /**
     * 格式化Date到字符串
     * @param date
     * @param format
     * @return
     */
    public static String formatDate(Date date, String format) {
        if(date == null || format == null || "".equals(format)){
            return "";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }
    public static String formatToDate(Date date){
        return formatDate(date, FORMAT_DATE);
    }
    public static String formatToDateTime(Date date){
        return formatDate(date, FORMAT_DATE_TIME);
    }

    /**
     * 获取当前时间后amount个月的日期
     * @param date
     * @param amount
     * @return
     */
    public static Date addDate(Date date,int amount){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.MONTH, amount);//把日期往后增加一个月.整数往后推,负数往前移动
        return calendar.getTime();
    }

    /**
     * 获取当前时间后amount天的日期
     * @param date
     * @param amount
     * @return
     */
    public static Date addDay(Date date, int amount){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, amount);//把日期往后增加天数,正数往后推,负数往前移
        return calendar.getTime();
    }

    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1,Date date2)
    {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
        return days;
    }

    /**
     * 判断时间是不是今天
     * @param date
     * @return    是返回true，不是返回false
     */
    public static boolean isNow(Date date) {
        //当前时间
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        //获取今天的日期
        String nowDay = sf.format(now);
        //对比的时间
        String day = sf.format(date);
        return day.equals(nowDay);
    }

    //测试方法
    public static void main(String[] ag){
        System.out.println(DateUtils.isNow(new Date(1530099269000L)));
        //System.out.println(DateUtils.getLongToDateTime(DateUtils.addDate(new Date(),1).getTime()));

    }
}
